﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SaleStat.COM.Exceptions;
using SaleStat.BLL.PS;
using SaleStat.COM.Constant;

public partial class PS001F001 : BasePage
{
    #region 初始化
    /// <summary>
    /// 初始化
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                //用户姓名焦点设置
                ddlPermission.Focus();
                getUserInfo();
                string strUserID = Session[SessionKeyConstant.SESSION_USERID].ToString();
                hdnUserID.Value = strUserID;
            }
        }
        catch (Exception ex)
        {
            ExceptionHandler(ex);

        }
    }
    #endregion

    #region 检索用户权限
    /// <summary>
    /// 检索用户权限
    /// </summary>
    private void getUserInfo()
    {
        PS001F001_BLL usertService = new PS001F001_BLL();
        DataTable dtProductSaleInfo = new DataTable();
        dtProductSaleInfo = usertService.getUserInfo();
        if (dtProductSaleInfo == null || dtProductSaleInfo.Rows.Count == 0)
        {
            throw new BusinessException("用户不存在!");
        }

        gvProductInfo.DataSource = dtProductSaleInfo;
        gvProductInfo.DataBind();
        ViewState["dtProductInfo"] = dtProductSaleInfo;
    }
    #endregion

    #region 显示条数选择事件
    /// <summary>
    /// 显示条数选择事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpShowLine_SelectedIndexChanged(object sender, EventArgs e)
    {
        //pagesize位dropdownlist中选择的值
        gvProductInfo.PageSize = Convert.ToInt32(drpShowLine.SelectedValue.Trim());
        //如果没有数据
        if (ViewState["dtProductInfo"] == null)
        {
            return;
        }
        gvProductInfo.PageIndex = 0;
        gvProductInfo.DataSource = (DataTable)ViewState["dtProductInfo"];
        gvProductInfo.DataBind();
    }
    #endregion

    #region 行绑定事件
    /// <summary>
    /// 行绑定事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvProductInfo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            //当前行是数据行的时候 
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //鼠标放上去显高亮
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='lightcyan';" + "this.style.cursor='hand'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                //删除linkButton
                LinkButton lbtnDelete = (LinkButton)e.Row.FindControl("lnkBtnDelete");
                lbtnDelete.CommandArgument = e.Row.RowIndex.ToString();
                LinkButton lbtnModify = (LinkButton)e.Row.FindControl("lnkBtnModify");
                lbtnModify.CommandArgument = e.Row.RowIndex.ToString();
                LinkButton lbtnUpdate = (LinkButton)e.Row.FindControl("lnkBtnUpdate");
                lbtnUpdate.CommandArgument = e.Row.RowIndex.ToString();

                // 备注信息大于15位时显示14位+"..."
                string strNote = e.Row.Cells[3].Text;
                if (strNote.Length > 18)
                {
                    e.Row.Cells[3].Text = strNote.Substring(0, 18) + "...";
                    e.Row.Cells[3].Attributes.Add("title", strNote);
                }
                string strUserID = e.Row.Cells[0].Text;
                if ("UI0001".Equals(strUserID))
                {
                    lbtnDelete.Enabled = false;
                    lbtnModify.Enabled = false;
                }
            }
        }
        catch (Exception ex)
        {
            ExceptionHandler(ex);
        }
    }
    #endregion

    #region 分页按钮事件
    /// <summary>
    /// 分页按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvProductInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            if (ViewState["dtProductInfo"] == null)
            {
                return;
            }
            //pageindex为默认值
            gvProductInfo.PageIndex = e.NewPageIndex;
            gvProductInfo.PageSize = Convert.ToInt32(drpShowLine.SelectedValue);
            gvProductInfo.DataSource = (DataTable)ViewState["dtProductInfo"];
            gvProductInfo.DataBind();

        }
        catch (Exception ex)
        {
            ExceptionHandler(ex);
        }
    }
    #endregion

    #region ROW_command事件
    /// <summary>
    /// ROW_command事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvProductInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (!"Page".Equals(e.CommandName))
            {
                DataTable dtProductInfo = (DataTable)ViewState["dtProductInfo"];
                if ("updateInfo".Equals(e.CommandName))
                {
                    //当前行索引取得
                    string strRow = e.CommandArgument.ToString();
                    int intIndex = Convert.ToInt32(strRow);
                    //行版本的取得
                    HiddenField hdnRow = (HiddenField)gvProductInfo.Rows[intIndex].Cells[4].FindControl("hdnVersion");
                    hdnRowver.Value = hdnRow.Value;
                    HiddenField hdnAdd = (HiddenField)gvProductInfo.Rows[intIndex].Cells[0].FindControl("hdCpID");
                    hdnAddUserID.Value = hdnAdd.Value;

                    for (int i = 0; i < dtProductInfo.Rows.Count; i++)
                    {
                        if (hdnAddUserID.Value.Equals(dtProductInfo.Rows[i]["USERID"].ToString()))
                        {
                            txtCustomName.Text = dtProductInfo.Rows[i]["UserName"].ToString();
                            ddlPermission.SelectedValue = dtProductInfo.Rows[i]["PermissionName"].ToString();
                            txtNote.Text = dtProductInfo.Rows[i]["Note"].ToString();
                        }
                    }
                    imgUpdate.Enabled = true;
                    imgAdd.Enabled = false;
                    hdnMode.Value = "2";
                }
                else if ("deleteInfo".Equals(e.CommandName))
                {
                    //当前行索引取得
                    string strRow = e.CommandArgument.ToString();
                    int intIndex = Convert.ToInt32(strRow);
                    //行版本的取得
                    HiddenField hdnRow = (HiddenField)gvProductInfo.Rows[intIndex].Cells[4].FindControl("hdnVersion");
                    hdnRowver.Value = hdnRow.Value;
                    HiddenField hdnAdd = (HiddenField)gvProductInfo.Rows[intIndex].Cells[0].FindControl("hdCpID");
                    hdnAddUserID.Value = hdnAdd.Value;

                    for (int i = 0; i < dtProductInfo.Rows.Count; i++)
                    {
                        if (hdnAddUserID.Value.Equals(dtProductInfo.Rows[i]["USERID"].ToString()))
                        {
                            hdnUserName.Value = dtProductInfo.Rows[i]["UserName"].ToString();

                            hdnPermissionName.Value = dtProductInfo.Rows[i]["PermissionName"].ToString();

                            if ("管理员".Equals(hdnPermissionName.Value))
                            {
                                hdnPermissionID.Value = "0";
                            }
                            else if ("系统维护员".Equals(hdnPermissionName.Value))
                            {
                                hdnPermissionID.Value = "1";
                            }
                            else if ("送货单录入员".Equals(hdnPermissionName.Value))
                            {
                                hdnPermissionID.Value = "2";
                            }
                            hdnNote.Value = dtProductInfo.Rows[i]["Note"].ToString();
                        }
                    }

                    hdnMode.Value = "3";
                    ArrayList arrList = getParamList();
                    PS001F001_BLL usertService = new PS001F001_BLL();
                    int intCount = usertService.updateUserInfo(arrList);
                    if (intCount == 0)
                    {
                        throw new BusinessException("用户删除失败!");
                    }

                    getUserInfo();
                    hdnUserName.Value = "";
                    hdnPermissionName.Value = "";
                    hdnPermissionID.Value = "";
                    hdnAddUserID.Value = "";
                    hdnRowver.Value = "";
                    ddlPermission.Focus();
                    hdnMode.Value = "1";
                }
                else if ("updatePasswordInfo".Equals(e.CommandName))
                {
                    //当前行索引取得
                    string strRow = e.CommandArgument.ToString();
                    int intIndex = Convert.ToInt32(strRow);
                    //行版本的取得
                    HiddenField hdnRow = (HiddenField)gvProductInfo.Rows[intIndex].Cells[4].FindControl("hdnVersion");
                    hdnRowver.Value = hdnRow.Value;
                    HiddenField hdnAdd = (HiddenField)gvProductInfo.Rows[intIndex].Cells[0].FindControl("hdCpID");
                    hdnAddUserID.Value = hdnAdd.Value;

                    for (int i = 0; i < dtProductInfo.Rows.Count; i++)
                    {
                        if (hdnAddUserID.Value.Equals(dtProductInfo.Rows[i]["USERID"].ToString()))
                        {
                            hdnUserName.Value = dtProductInfo.Rows[i]["UserName"].ToString();

                            hdnPermissionName.Value = dtProductInfo.Rows[i]["PermissionName"].ToString();

                            if ("管理员".Equals(hdnPermissionName.Value))
                            {
                                hdnPermissionID.Value = "0";
                            }
                            else if ("系统维护员".Equals(hdnPermissionName.Value))
                            {
                                hdnPermissionID.Value = "1";
                            }
                            else if ("送货单录入员".Equals(hdnPermissionName.Value))
                            {
                                hdnPermissionID.Value = "2";
                            }
                            hdnNote.Value = dtProductInfo.Rows[i]["Note"].ToString();
                        }
                    }

                    hdnMode.Value = "4";
                    ArrayList arrList = getParamList();
                    PS001F001_BLL usertService = new PS001F001_BLL();
                    int intCount = usertService.updateUserInfo(arrList);
                    if (intCount == 0)
                    {
                        throw new BusinessException("用户密码更新失败!");
                    }

                    hdnUserName.Value = "";
                    hdnAddUserID.Value = "";
                    hdnPermissionName.Value = "";
                    hdnPermissionID.Value = "";
                    hdnRowver.Value = "";
                    ddlPermission.Focus();
                    hdnMode.Value = "1";
                }
            }
        }
        catch (Exception ex)
        {
            ExceptionHandler(ex);
        }

    }
    #endregion

    #region 添加权限用户
    /// <summary>
    /// 添加权限用户
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void imgAdd_Click(object sender, ImageClickEventArgs e)
    {
        ArrayList arrList = getParamList();
        PS001F001_BLL usertService = new PS001F001_BLL();
        int intCount = usertService.insertProductInfo(arrList);
        if (intCount == 0)
        {
            throw new BusinessException("用户权限添加失败!");
        }
        txtNote.Text = "";
        ddlPermission.SelectedValue = "0";
        txtCustomName.Text = "";
        hdnAddUserID.Value = "";
        hdnRowver.Value = "";
        getUserInfo();
        ddlPermission.Focus();
    }

    private ArrayList getParamList()
    {
        ArrayList arrList = new ArrayList();

        if ("1".Equals(hdnMode.Value))
        {
            PS001F001_BLL usertService = new PS001F001_BLL();
            string strUserID = usertService.getMaxProductID();
            arrList.Add(strUserID);
        }
        else
        {
            arrList.Add(hdnAddUserID.Value);
        }

        if ("2".Equals(hdnMode.Value) || "1".Equals(hdnMode.Value))
        {
            arrList.Add(ddlPermission.SelectedValue);
            arrList.Add(txtCustomName.Text.Trim());
            arrList.Add("123456");
            arrList.Add(txtNote.Text.Trim());
        }
        else
        {
            arrList.Add(hdnPermissionID.Value);
            arrList.Add(hdnUserName.Value);
            arrList.Add("123456");
            arrList.Add(hdnNote.Value);
        }


        if ("3".Equals(hdnMode.Value))
        {
            arrList.Add("FALSE");
        }
        else
        {
            arrList.Add("TRUE");
        }
        if ("1".Equals(hdnMode.Value))
        {
            arrList.Add(hdnUserID.Value);
        }
        arrList.Add(hdnUserID.Value);

        if (!"1".Equals(hdnMode.Value))
        {
            arrList.Add(hdnRowver.Value);
        }

        return arrList;
    }
    #endregion

    #region 更新权限
    /// <summary>
    /// 更新权限
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void imgUpdate_Click(object sender, ImageClickEventArgs e)
    {
        ArrayList arrList = getParamList();
        PS001F001_BLL usertService = new PS001F001_BLL();
        int intCount = usertService.updateUserInfo(arrList);
        if (intCount == 0)
        {
            throw new BusinessException("用户权限更新失败!");
        }
        imgUpdate.Enabled = false;
        imgAdd.Enabled = true;
        txtNote.Text = "";
        ddlPermission.SelectedValue = "0";
        txtCustomName.Text = "";
        hdnAddUserID.Value = "";
        hdnRowver.Value = "";
        getUserInfo();
        ddlPermission.Focus();
        hdnMode.Value = "1";
    }
    #endregion

    #region 重置按钮事件
    /// <summary>
    /// 重置按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void imgReset_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            txtNote.Text = "";
            ddlPermission.SelectedValue = "0";
            txtCustomName.Text = "";
            hdnAddUserID.Value = "";
            hdnRowver.Value = "";
            ddlPermission.Focus();
            hdnMode.Value = "1";
            imgAdd.Enabled = true;
            imgUpdate.Enabled = false;
            lblMsg.Visible = false;
        }
        catch (Exception ex)
        {
            ExceptionHandler(ex);
        }
    }
    #endregion
}
